package com.id.coinscleaner

<<<<<<< HEAD
=======
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
>>>>>>> 65dd7ef4291bce327bf4a6acb247be4d8b45e92b
import android.widget.Toast
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.id.coinscleaner.common.ViewBindingFragment
import com.id.coinscleaner.databinding.FragmentRetainBinding
import com.id.coinscleaner.utils.Events
import com.id.coinscleaner.utils.SubSku
import com.id.coinscleaner.utils.UserCanceledPayException
import com.id.coinscleaner.utils.pay
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

<<<<<<< HEAD

=======
>>>>>>> 65dd7ef4291bce327bf4a6acb247be4d8b45e92b
class RetainFragment : ViewBindingFragment<FragmentRetainBinding>() {
    override fun initView() {
        super.initView()
        binding.continueBtn.setOnClickListener {
            purchaseTrail()
        }
        binding.trailBtn.setOnClickListener {
            purchaseTrail()
        }
        binding.retainFragmentBg.setOnClickListener {
            this.requireActivity().onBackPressed()
        }
    }

<<<<<<< HEAD

    fun purchaseTrail() {
        lifecycleScope.launch {
            Events.sendEvent("canceled_paywall_continue${SubSku.SkuTrail.skuId}")
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                this@RetainFragment.requireActivity().pay(SubSku.SkuTrail).catch { e ->
                    if (e !is UserCanceledPayException) {
                        Events.sendEvent(
                            "canceled_paywall_sku_fail_${SubSku.SkuTrail.skuId}",
=======
    fun purchaseTrail() {
        //TODO 改变挽留页面相关配置
        lifecycleScope.launch {
            Events.sendEvent("canceled_paywall_continue${SubSku.Sku1Yearly.skuId}")
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                this@RetainFragment.requireActivity().pay(SubSku.Sku1Yearly).catch { e ->
                    if (e !is UserCanceledPayException) {
                        Events.sendEvent(
                            "canceled_paywall_sku_fail_${SubSku.Sku1Yearly.skuId}",
>>>>>>> 65dd7ef4291bce327bf4a6acb247be4d8b45e92b
                            "reason" to e.message
                        )
                        Toast.makeText(
                            this@RetainFragment.requireActivity(),
                            "Network Error",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }.collectLatest {
<<<<<<< HEAD
                    Events.sendEvent("canceled_paywall_sku_success_${SubSku.SkuTrail.skuId}")
=======
                    Events.sendEvent("canceled_paywall_sku_success_${SubSku.Sku1Yearly.skuId}")
>>>>>>> 65dd7ef4291bce327bf4a6acb247be4d8b45e92b
                }
            }
        }
    }
<<<<<<< HEAD

=======
>>>>>>> 65dd7ef4291bce327bf4a6acb247be4d8b45e92b
}